package com.duobaoyu.dby.supply.biz.client.boundary.message.handler;

import com.duobaoyu.dby.channel.adapter.constant.ChannelConstant;
import com.duobaoyu.dby.supply.biz.client.boundary.message.AbstractSupplyMessageHandlerStrategy;
import com.duobaoyu.dby.supply.biz.client.boundary.message.bean.pojo.MessagePayload;
import com.duobaoyu.dby.supply.biz.producer.SupplyChainMQProducer;
import com.duobaoyu.dby.supply.core.framework.annotation.Default;
import com.duobaoyu.dby.supply.db.constant.SupplyMapperConstant;
import com.duobaoyu.dby.supply.db.entity.message.ChannelMessage;
import com.duobaoyu.dby.supply.db.entity.message.ChannelMessageOrder;
import com.duobaoyu.dby.supply.db.helper.DynamicTableNameHelper;
import com.google.common.collect.Maps;
import com.google.common.collect.Sets;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;

import java.util.Collection;
import java.util.Map;
import java.util.Set;

/**
 * @author liyang
 * @version 1.0.0
 * @ClassName DefaultSupplyMessageHandlerStrategy
 * @Description 默认消息处理器
 * @createTime 16:00:00 2023/09/27
 */
@Slf4j
@Default
@Component
@RequiredArgsConstructor
public class DefaultSupplyMessageHandlerStrategy extends AbstractSupplyMessageHandlerStrategy {

	private final SupplyChainMQProducer supplyChainMQProducer;

	@Override
	protected Set<String> messageLabels() {
		return Sets.newHashSet(ChannelConstant.ORDER_MESSAGE_LABEL,ChannelConstant.AFTER_SALE_MESSAGE_LABEL);
	}

	@Override
	protected Object beforeHandler(MessagePayload messagePayload) {
		Map<String, Object> map = Maps.newHashMap();
		map.put(SupplyMapperConstant.CHANNEL_MESSAGE_TABLE_NAME, SupplyMapperConstant.CHANNEL_MESSAGE_ORDER_SUFFIX);
		DynamicTableNameHelper.setRequestData(map);
		return ChannelMessageOrder.class;
	}

	@Override
	protected Object afterHandler(MessagePayload messagePayload, Collection<ChannelMessage> channelMessages) {
		if(CollectionUtils.isNotEmpty(channelMessages)) {
			for (ChannelMessage channelMessage : channelMessages) {
				if(StringUtils.isNotBlank(channelMessage.getId())) {
					supplyChainMQProducer.channelMessageSend(channelMessage.getId());
				}
			}
		}
		return messagePayload;
	}
}
